from datetime import timezone

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select, insert, desc
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.responses import JSONResponse

from auth.base_config import get_current_user
from auth.models import User
from database import get_async_session
from salaries.crud import get_salary
from salaries.models import salaries, Salary
from salaries.schemas import SalariesCreate, SalariesRead

router = APIRouter(
    prefix="/salaries",
    tags=["salaries"]
)


@router.get("/", response_model=SalariesRead)
async def get_info(
        session: AsyncSession = Depends(get_async_session),
        current_user: User = Depends(get_current_user)
):
    response = await get_salary(session, current_user)

    return response

@router.post("/")
async def create_salary(
    sal: SalariesCreate,
        session: AsyncSession = Depends(get_async_session),
        current_user: User = Depends(get_current_user)
):
    if not current_user.is_superuser:
        raise HTTPException(status_code=403, detail="Not rules")

    sal.raising_data = sal.raising_data.astimezone(timezone.utc).replace(tzinfo=None)
    stmt = insert(Salary).values(**sal.dict())
    await session.execute(stmt)
    await session.commit()

    return JSONResponse(content={"message": "created"})

